<style>
    .layui-input-inline{margin-top:10px;}
</style>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a>结算管理</a>
        <a><cite>结算记录</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">结算记录列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show" style="margin-bottom:1%;">
                    <div class="layui-row" style="float:right; margin-bottom: 10px;">
                        <div class="layui-form" style="float:right;">
                            <div id="timeDiv" class="layui-form-item" style="margin:0;">
                                <div class="layui-input-inline">
                                    <input type="text" name="infoId" id="searchInfoId" placeholder="结算商ID" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline">
                                    <input type="text" name="accountName" id="searchAccountName" placeholder="账户名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline">
                                    <input type="text" name="settOrderId" id="searchSettOrderId" placeholder="结算单号" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline">
                                    <select name="settStatus" id="searchSettStatus" lay-search="">
                                        <option value="-99">状态</option>
                                        <option value="1">等待审核</option>
                                        <option value="2">已审核</option>
                                        <option value="3">审核不通过</option>
                                        <option value="4">打款中</option>
                                        <option value="5">打款成功</option>
                                        <option value="6">打款失败</option>
                                    </select>
                                </div>
                                <div class="layui-input-inline">
                                    <label class="layui-form-label">自动刷新</label>
                                    <input type="checkbox" lay-filter="offlineRechargeAutoRefCheckbox" checked lay-skin="switch" />
                                </div>
                                <div class="layui-input-inline">
                                    <a id="search" offlineRechargeAutoRef="true" subTime="9" initSubTime="9" class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i>搜索</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <blockquote class="layui-elem-quote" id="amountTip">
                            订单数:<sapn id="allTotalCount" style="color: blue; margin-right: 10px;"></sapn>
                            结算金额:<span id="allTotalAmount" style="color: blue; margin-right: 10px;"></span>
                            结算费用:<span id="allTotalFee" style="color: green; margin-right: 10px;"></span>
                        </blockquote>
                    </div>
                </div>
                <table id="jsList" lay-filter="jsList"></table>
            </div>
        </div>
    </div>
</div>
<script>

     //标题内容模板
     var tplTitle = function(d){
         return "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='detail'><i class='layui-icon layui-icon-about'></i>查看</a>";
     };
     var tplStatus = function(d){
         if(d.settStatus == 1) {
             return "<span style='color: red'>等待审核</span>";
         }else if(d.settStatus == 2) {
             return "<span style='color: darkgreen'>已审核</span>";
         }else if(d.settStatus == 3) {
             return "<span style='color: indigo'>审核不通过</span>";
         }else if(d.settStatus == 4) {
             return "<span style='color: blue'>打款中</span>";
         }else if(d.settStatus == 5) {
             return "<span style='color: brown'>打款成功</span>";
         }else if(d.settStatus == 6) {
             return "<span style='color: #122b40'>打款失败</span>";
         }
     };

     layui.use(['table','util','element', 'admin', 'form', 'timePick'], function(){
        var table = layui.table
        ,$ = layui.$
        ,admin = layui.admin
        ,form = layui.form
        ,timePick = layui.timePick
        ,view = layui.view
        ,element = layui.element;

        timePick.init();  //第一个参数表示开始时间   第二个表示结束时间   0,0今天   -1,-1昨天   -7,0近一周   以此类推
        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

         $.setCountResult = function(infoId, accountName, settOrderId, settStatus, createTimeStart, createTimeEnd) {
             admin.req({
                 type: 'get',
                 url: layui.setter.baseUrl + '/sett/count',
                 data: {
                     infoId : infoId,
                     accountName : accountName,
                     settOrderId : settOrderId,
                     settStatus : settStatus,
                     createTimeStart : createTimeStart,
                     createTimeEnd : createTimeEnd
                 },
                 error: function(err){
                     layer.alert(JSON.stringify(err.field), {
                         title: '错误提示'
                     })
                 },
                 success: function(res){
                     if(res.code == 0){
                         $('#allTotalCount').html(res.data.allTotalCount);
                         $('#allTotalAmount').html("￥" + res.data.allTotalAmount/100.00);
                         $('#allTotalFee').html("￥" + res.data.allTotalFee/100.0);
                     }
                 }
             });
         };

         // 初始化统计结果
         var createTimeStart = $('#createTimeStart').val();
         var createTimeEnd = $('#createTimeEnd').val();
         var settOrderId = $('#searchSettOrderId').val();
         var infoId = $("#searchInfoId").val();
         var accountName = $('#searchAccountName').val().trim();
         var settStatus = $("#searchSettStatus").val();
         $.setCountResult(infoId, accountName, settOrderId, settStatus, createTimeStart, createTimeEnd);

        table.render({
            elem: '#jsList'
            ,url: layui.setter.baseUrl + '/sett/list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token,
                createTimeStart: createTimeStart,
                createTimeEnd: createTimeEnd
            }
            ,id: 'tableReload'
            ,cols: [[
                {field:'settOrderId', width: 220, title: '结算单号', sort: true }
                ,{field:'infoType', title: '结算商类型', templet: '<div>{{d.infoType == 1?"代理商":d.infoType == 2?"商户":d.infoType == 3?"":""}}</div>' }
                ,{field:'infoId', title: '结算商ID' }
                /*,{field:'settType',title: '结算类型',templet: '<div>{{d.settType == 1?"手工结算":d.settType == 2?"自动结算":""}}</div>'}*/
                ,{field:'accountName', title: '账户名' }
                ,{field:'settAmount', title: '结算金额', templet: '<div>{{d.settAmount/100}}</div>' }
                ,{field:'settFee', title: '手续费', templet: '<div>{{d.settFee/100}}</div>' }
                ,{field:'remitAmount', title: '打款金额', templet: '<div>{{d.remitAmount/100}}</div>' }
                ,{field:'settStatus', title: '结算状态', templet: tplStatus}
                ,{field:'settDate', title: '结算日期', width: 150, templet: '<div>{{ layui.util.toDateString(d.settDate, "yyyy-MM-dd") }}</div>'}
                ,{field:'edite', title: '操作', width: 100, fixed: 'right', templet: tplTitle }
            ]]
            ,page: true
            ,skin: 'line'
        });

         //监听工具条
         table.on('tool(jsList)', function(obj){
             var data = obj.data;
             if(obj.event === 'detail'){
                 view.xxpayPopup("查看", "sett/detail", {id: data.id}, {btn: false});
             }
         });

        // 搜索
        var $ = layui.$, active = {
            reload: function(){
                var createTimeStart = $('#createTimeStart').val();
                var createTimeEnd = $('#createTimeEnd').val();
                var settOrderId = $('#searchSettOrderId').val();
                var infoId = $("#searchInfoId").val();
                var accountName = $('#searchAccountName').val().trim();
                var settStatus = $("#searchSettStatus").val();
                $.setCountResult(infoId, accountName, settOrderId, settStatus, createTimeStart, createTimeEnd);
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        settOrderId: settOrderId,
                        infoId : infoId,
                        accountName: accountName,
                        createTimeStart : createTimeStart,
                        createTimeEnd : createTimeEnd,
                        settStatus : settStatus
                    }
                });
            }
        };
        $('#search').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

         form.on("switch(offlineRechargeAutoRefCheckbox)", function(data){
             if(data.elem.checked){ //开启
                 $("#search[offlineRechargeAutoRef]").attr("offlineRechargeAutoRef", "true");
             }else{
                 $("#search[offlineRechargeAutoRef]").attr("offlineRechargeAutoRef", "false");
             }
         });

        // 渲染表单
        form.render();

     })

     //定时刷新  定时任务,  先清除， 后新增
     if(typeof(alipayCashInterval) != 'undefined'){
         window.clearInterval(alipayCashInterval);
     }

     var alipayCashInterval = window.setInterval('processRef()', 1000);  //每秒执行

     function processRef(){ //处理刷新任务

         var searchBtn = layui.jquery("#search[offlineRechargeAutoRef]");
         if(searchBtn.length > 0){

             if(searchBtn.attr("offlineRechargeAutoRef") != "true"){ //未开启
                 return searchBtn.html('<i class="layui-icon layui-icon-search"></i>'+"搜索");
             }

             //开启定时刷新
             var subTime = searchBtn.attr('subTime') * 1;
             if(subTime <= 0){
                 layui.jquery('#search').click();
                 subTime = searchBtn.attr('initSubTime') * 1;
             }
             searchBtn.html('<i class="layui-icon layui-icon-search"></i>'+"搜索（"+subTime+"s）");
             subTime--;
             searchBtn.attr('subTime', subTime);

         }else{   //当查询控件不存在不在内，说明不是本页面，删除该定时任务器
             window.clearInterval(alipayCashInterval);
         }
     }
</script>